<?php
class MAsin extends MY_Model {

	const STATE_DISABLED = 1;
	const STATE_ENABLED = 2;
	
	//排序方式
	const ORDER_ID_DESC = 1;
	const ORDER_ID_ASC = 2;
	const ORDER_EMAIL_NUMS_DESC = 3;
	const ORDER_EMAIL_NUMS_ASC = 4;
	const ORDER_CREATE_TIME_DESC = 5;
	const ORDER_CREATE_TIME_ASC = 6;
	
	protected $table = 'asin';
	private $filter = array();

    function __construct() {
        parent::__construct();
    }
    
    public static function getState($state=NULL){
        $define = array(self::STATE_DISABLED=>'可用',self::STATE_ENABLED=>'无效');
    	if($state === NULL){
    		return $define;
    	}else{
    		return key_exists($state, $define)?$define[$state]:'';
    	}
    }
    
    public static function getOrderType($orderType=NULL){
        $define = array(
            self::ORDER_ID_DESC=>'ID倒序',
            self::ORDER_ID_ASC=>'ID升序',
            self::ORDER_EMAIL_NUMS_DESC=>'邮箱个数倒序',
            self::ORDER_EMAIL_NUMS_ASC=>'邮箱个数升序',
            self::ORDER_CREATE_TIME_DESC=>'创建时间倒序',
            self::ORDER_CREATE_TIME_ASC=>'创建时间升序'
            
        );
        if($orderType === NULL){
            return $define;
        }else{
            return key_exists($state, $define)?$define[$state]:'';
        }
    }
    
	public function getList($limit, $sort=false){
		$this->db->select( 'a.*,count(e.id) email_num');
		$this->db->distinct();
		$this->db->from($this->table.'  a');
		$this->db->join('core_emails e','a.asin = e.asin','LEFT');
		$this->db->limit($limit['limit'],$limit['offset']);
		$this->formfilter->doFilter();
		$this->db->group_by('a.asin');
		if($sort){
		    switch ($sort){
		        case self::ORDER_ID_DESC:
		            $this->db->order_by('id', 'desc');
		            break;
		        case self::ORDER_ID_ASC:
		            $this->db->order_by('id', 'asc');
		            break;
		        case self::ORDER_EMAIL_NUMS_DESC:
		            $this->db->order_by('email_num', 'desc');
		            break;
		        case self::ORDER_EMAIL_NUMS_ASC:
		            $this->db->order_by('email_num', 'asc');
		            break;
		        case self::ORDER_CREATE_TIME_DESC:
		            $this->db->order_by('a.create_time', 'desc');
		            break;
		        case self::ORDER_CREATE_TIME_ASC:
		            $this->db->order_by('a.create_time', 'asc');
		            break;
		    }
		}else{
		    $this->db->order_by('id', 'desc');
		}
		$query = $this->db->get();
		return $query->result();
	}
	
	public function getCount(){
		$this->db->select('count(distinct a.id) as num');
		$this->db->from($this->table . ' as a');
		$this->db->join('core_emails e','a.asin = e.asin','LEFT');
		$this->formfilter->doFilter();
		//$this->db->group_by('a.asin');
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}
	
	//得到全部数量
	public function getTotalCount(){
		$this->db->select('count(id) as num');
		$this->db->from($this->table);
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}
	
	public function getById($id){
		$query = $this->db->where('id',$id)->get($this->table);
		return $query->row();
	}
	
	
}
